﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace VisualImageTest
{
	/// <summary>
	/// VisualImage
	/// </summary>
	public class VisualImage : Control
	{
		#region Visual DependencyProperty

		public static readonly DependencyProperty VisualProperty = DependencyProperty.Register(
			"Visual",
			typeof( FrameworkElement ),
			typeof( VisualImage ),
			new PropertyMetadata( OnVisualChanged ) );

		public FrameworkElement Visual
		{
			get { return (FrameworkElement)GetValue( VisualProperty ); }
			set { SetValue( VisualProperty, value ); }
		}

		private static void OnVisualChanged( DependencyObject obj, DependencyPropertyChangedEventArgs args )
		{
			var visualImage = obj as VisualImage;
			visualImage.OnVisualChanged( args );
		}

		private void OnVisualChanged( DependencyPropertyChangedEventArgs args )
		{
			if ( args.OldValue != null ) ( (FrameworkElement)args.OldValue ).SizeChanged -= VisualImage_SizeChanged;
			if ( args.NewValue != null )
			{
				var visual = (FrameworkElement)args.NewValue;
				visual.SizeChanged += VisualImage_SizeChanged;
				PrepareBitmap( (int)visual.RenderSize.Width, (int)visual.RenderSize.Height );
			}
		}

		private void VisualImage_SizeChanged( object sender, SizeChangedEventArgs e )
		{
			PrepareBitmap( (int)e.NewSize.Width, (int)e.NewSize.Height );
		}

		#endregion // Visual DependencyProperty

		#region Bitmap DependencyProperty

		public static readonly DependencyProperty BitmapProperty = DependencyProperty.Register(
			"Bitmap",
			typeof( WriteableBitmap ),
			typeof( VisualImage ),
			null );

		public WriteableBitmap Bitmap
		{
			get { return (WriteableBitmap)GetValue( BitmapProperty ); }
			set { SetValue( BitmapProperty, value ); }
		}

		#endregion // Bitmap DependencyProperty

		#region ImageBrush DependencyProperty

		public static readonly DependencyProperty ImageBrushProperty = DependencyProperty.Register(
			"ImageBrush",
			typeof( ImageBrush ),
			typeof( VisualImage ),
			new PropertyMetadata( OnImageBrushChanged ) );

		public ImageBrush ImageBrush
		{
			get { return (ImageBrush)GetValue( ImageBrushProperty ); }
			set { SetValue( ImageBrushProperty, value ); }
		}

		private static void OnImageBrushChanged( DependencyObject obj, DependencyPropertyChangedEventArgs args )
		{
			var visualImage = obj as VisualImage;
			visualImage.Invalidate();
		}

		#endregion // VisualBrush DependencyProperty

		/// <summary>
		/// Initializes a new instance of the <see cref="VisualImage"/> class.
		/// </summary>
		public VisualImage()
		{
		}

		/// <summary>
		/// Prepares the bitmap.
		/// </summary>
		/// <param name="width">The width.</param>
		/// <param name="height">The height.</param>
		private void PrepareBitmap( int width, int height )
		{
			if ( width > 0 && height > 0 )
			{
				Bitmap = new WriteableBitmap( width, height );
				Invalidate();

				if ( ImageBrush != null ) ImageBrush.ImageSource = Bitmap;
			}
		}

		/// <summary>
		/// Invalidates the VisualImage and causes WriteableBitmap to be refreshed.
		/// </summary>
		public void Invalidate()
		{
			if ( Bitmap != null && Visual != null )
			{
				Array.Clear( Bitmap.Pixels, 0, Bitmap.Pixels.Length );
				Bitmap.Render( Visual, Visual.RenderTransform );
				Bitmap.Invalidate();
			}
		}
	}
}